﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Constriction.Constrict
{
    public class ConstrictBlock
    {
        public const int CONSTRICT_BLOCK_SIZE = 16;

        public Rectangle boundedBox;

        private ConstrictBlockSideEnum side;
        private float xPos;

        public float constrictSpeed;
        public bool isMoving;
        public bool movingOut;

        public ConstrictBlock(int x, int y, ConstrictBlockSideEnum side)
        {
            boundedBox = new Rectangle(x, y, CONSTRICT_BLOCK_SIZE, CONSTRICT_BLOCK_SIZE);
            this.side = side;
            xPos = 0;

            constrictSpeed = 100;
            movingOut = true;
            isMoving = false;
        }

        public void Update(GameTime time)
        {
            UpdateWidth(time);
            CheckState();
        }

        private void UpdateWidth(GameTime time)
        {
            if (isMoving)
            {
                if (movingOut)
                {
                    xPos += constrictSpeed * (float)time.ElapsedGameTime.TotalSeconds;
                }
                else
                {
                    xPos -= constrictSpeed * (float)time.ElapsedGameTime.TotalSeconds;
                }

                if (xPos < 0)
                {
                    xPos = 0;
                }

                if (side == ConstrictBlockSideEnum.Left)
                {
                    boundedBox.X = (int)xPos;
                }
                else
                {
                    boundedBox.X = MainGame.SCREEN_WIDTH - MainGame.WALL_SIZE - (int)xPos;
                }
            }
        }

        private void CheckState()
        {
            if (xPos == 0 && !movingOut)
            {
                isMoving = false;
            }
        }

        public void Draw(SpriteBatch batch)
        {
            batch.Draw(MainGame.emptyTex, boundedBox, Color.Black);
        }
    }
}
